#include <iostream>
using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    explicit TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
public:
    static string tree2str(TreeNode* root)
    {
        if(root == nullptr)
            return "";

        string rs = to_string(root->val);
        // 1.左不为空，肯定有括号
        // 2.左为空，但是右边不为空，左边括号要保留
        if(root->left != nullptr || root->right != nullptr)
        {
            rs += "(";
            rs += tree2str(root->left);
            rs += ")";
        }
        //左为空，且右不为空，上面已经把左边的括号保留了，这儿就只考虑右边
        if(root->right != nullptr)
        {
            rs += "(";
            rs += tree2str(root->right);
            rs += ")";
        }
        // 直接跳到这说明左右都为空，rs也自然没加括号
        return rs;
    }
};

int main()
{
    return 0;
}
